home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / reuse.lha / reuse / m2c / Strings.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  11KB  |  662 lines

  1. #include "SYSTEM_.h"
  2.  
  3. #ifndef DEFINITION_General
  4. #include "General.h"
  5. #endif
  6.  
  7. #ifndef DEFINITION_IO
  8. #include "IO.h"
  9. #endif
  10.  
  11. #ifndef DEFINITION_IO
  12. #include "IO.h"
  13. #endif
  14.  
  15. #ifndef DEFINITION_Strings
  16. #include "Strings.h"
  17. #endif
  18.  
  19.  
  20. #define EolCh    '\n'
  21. static BOOLEAN reported;
  22. static struct S_1 {
  23.     CHAR A[15 + 1];
  24. } MyCHR;
  25. static void Error ARGS(());
  26. static CARDINAL Rank ARGS((CHAR ch));
  27. #define MaxInt    2147483647
  28. #define MaxIntDiv10    (MaxInt / 10)
  29. struct S_4 {
  30.     CHAR A[10 + 1];
  31. };
  32.  
  33.  
  34. static void Error
  35. # ifdef __STDC__
  36. ()
  37. # else
  38. ()
  39. # endif
  40. {
  41.   IO_WriteS((System_tFile)IO_StdError, (STRING)"string too long, max. 255", 25L);
  42.   IO_WriteNl((System_tFile)IO_StdError);
  43. }
  44.  
  45. void Strings_Assign
  46. # ifdef __STDC__
  47. (Strings_tString *s1, Strings_tString *s2)
  48. # else
  49. (s1, s2)
  50. Strings_tString *s1, *s2;
  51. # endif
  52. {
  53.   Strings_tStringIndex i;
  54.  
  55.   {
  56.     register Strings_tString *W_1 = s2;
  57.  
  58.     {
  59.       Strings_tStringIndex B_1 = 1, B_2 = W_1->Length;
  60.  
  61.       if (B_1 <= B_2)
  62.         for (i = B_1;; i += 1) {
  63.           s1->Chars.A[i] = W_1->Chars.A[i];
  64.           if (i >= B_2) break;
  65.         }
  66.     }
  67.     s1->Length = W_1->Length;
  68.   }
  69. }
  70.  
  71. void Strings_AssignEmpty
  72. # ifdef __STDC__
  73. (Strings_tString *s)
  74. # else
  75. (s)
  76. Strings_tString *s;
  77. # endif
  78. {
  79.   s->Length = 0;
  80.   reported = FALSE;
  81. }
  82.  
  83. void Strings_Concatenate
  84. # ifdef __STDC__
  85. (Strings_tString *s1, Strings_tString *s2)
  86. # else
  87. (s1, s2)
  88. Strings_tString *s1, *s2;
  89. # endif
  90. {
  91.   Strings_tStringIndex i;
  92.  
  93.   if (s1->Length + s2->Length > Strings_cMaxStrLength) {
  94.     Error();
  95.   } else {
  96.     {
  97.       register Strings_tString *W_2 = s1;
  98.  
  99.       {
  100.         Strings_tStringIndex B_3 = 1, B_4 = s2->Length;
  101.  
  102.         if (B_3 <= B_4)
  103.           for (i = B_3;; i += 1) {
  104.             W_2->Chars.A[W_2->Length + i] = s2->Chars.A[i];
  105.             if (i >= B_4) break;
  106.           }
  107.       }
  108.       INC1(W_2->Length, s2->Length);
  109.     }
  110.   }
  111. }
  112.  
  113. void Strings_Append
  114. # ifdef __STDC__
  115. (Strings_tString *s, CHAR c)
  116. # else
  117. (s, c)
  118. Strings_tString *s;
  119. CHAR c;
  120. # endif
  121. {
  122.   if (s->Length == Strings_cMaxStrLength) {
  123.     if (!reported) {
  124.       Error();
  125.       reported = TRUE;
  126.     }
  127.   } else {
  128.     INC(s->Length);
  129.     s->Chars.A[s->Length] = c;
  130.   }
  131. }
  132.  
  133. CARDINAL Strings_Length
  134. # ifdef __STDC__
  135. (Strings_tString *s)
  136. # else
  137. (s)
  138. Strings_tString *s;
  139. # endif
  140. {
  141.   return s->Length;
  142. }
  143.  
  144. BOOLEAN Strings_IsEqual
  145. # ifdef __STDC__
  146. (Strings_tString *s1, Strings_tString *s2)
  147. # else
  148. (s1, s2)
  149. Strings_tString *s1, *s2;
  150. # endif
  151. {
  152.   Strings_tStringIndex i;
  153.  
  154.   if (s1->Length != s2->Length) {
  155.     return FALSE;
  156.   } else {
  157.     {
  158.       Strings_tStringIndex B_5 = 1, B_6 = s1->Length;
  159.  
  160.       if (B_5 <= B_6)
  161.         for (i = B_5;; i += 1) {
  162.           if (s1->Chars.A[i] != s2->Chars.A[i]) {
  163.             return FALSE;
  164.           }
  165.           if (i >= B_6) break;
  166.         }
  167.     }
  168.   }
  169.   return TRUE;
  170. }
  171.  
  172. BOOLEAN Strings_IsInOrder
  173. # ifdef __STDC__
  174. (Strings_tString *s1, Strings_tString *s2)
  175. # else
  176. (s1, s2)
  177. Strings_tString *s1, *s2;
  178. # endif
  179. {
  180.   INTEGER i;
  181.   CARDINAL rank1, rank2;
  182.  
  183.   {
  184.     LONGINT B_7 = 1, B_8 = General_Min((LONGINT)s1->Length, (LONGINT)s2->Length);
  185.  
  186.     if (B_7 <= B_8)
  187.       for (i = B_7;; i += 1) {
  188.         rank1 = Rank(s1->Chars.A[i]);
  189.         rank2 = Rank(s2->Chars.A[i]);
  190.         if (rank1 < rank2) {
  191.           return TRUE;
  192.         } else if (rank1 > rank2) {
  193.           return FALSE;
  194.         }
  195.         if (i >= B_8) break;
  196.       }
  197.   }
  198.   return s1->Length <= s2->Length;
  199. }
  200.  
  201. static CARDINAL Rank
  202. # ifdef __STDC__
  203. (CHAR ch)
  204. # else
  205. (ch)
  206. CHAR ch;
  207. # endif
  208. {
  209.   return ORD(ch);
  210. }
  211.  
  212. void Strings_Exchange
  213. # ifdef __STDC__
  214. (Strings_tString *s1, Strings_tString *s2)
  215. # else
  216. (s1, s2)
  217. Strings_tString *s1, *s2;
  218. # endif
  219. {
  220.   Strings_tString temp;
  221.  
  222.   Strings_Assign(&temp, s1);
  223.   Strings_Assign(s1, s2);
  224.   Strings_Assign(s2, &temp);
  225. }
  226.  
  227. void Strings_SubString
  228. # ifdef __STDC__
  229. (Strings_tString *s1, Strings_tStringIndex from, Strings_tStringIndex to, Strings_tString *s2)
  230. # else
  231. (s1, from, to, s2)
  232. Strings_tString *s1;
  233. Strings_tStringIndex from, to;
  234. Strings_tString *s2;
  235. # endif
  236. {
  237.   Strings_tStringIndex i;
  238.  
  239.   {
  240.     register Strings_tString *W_3 = s2;
  241.  
  242.     W_3->Length = 0;
  243.     {
  244.       Strings_tStringIndex B_9 = from, B_10 = to;
  245.  
  246.       if (B_9 <= B_10)
  247.         for (i = B_9;; i += 1) {
  248.           INC(W_3->Length);
  249.           W_3->Chars.A[W_3->Length] = s1->Chars.A[i];
  250.           if (i >= B_10) break;
  251.         }
  252.     }
  253.   }
  254. }
  255.  
  256. CHAR Strings_Char
  257. # ifdef __STDC__
  258. (Strings_tString *s, Strings_tStringIndex i)
  259. # else
  260. (s, i)
  261. Strings_tString *s;
  262. Strings_tStringIndex i;
  263. # endif
  264. {
  265.   return s->Chars.A[i];
  266. }
  267.  
  268. void Strings_ArrayToString
  269. # ifdef __STDC__
  270. (CHAR a[], LONGCARD O_1, Strings_tString *s)
  271. # else
  272. (a, O_1, s)
  273. CHAR a[];
  274. LONGCARD O_1;
  275. Strings_tString *s;
  276. # endif
  277. {
  278.   Strings_tStringIndex i;
  279.   OPEN_ARRAY_LOCALS
  280.  
  281.   ALLOC_OPEN_ARRAYS(O_1 * sizeof(CHAR), 1)
  282.   COPY_OPEN_ARRAY(a, O_1, CHAR)
  283.   i = 0;
  284.   for (;;) {
  285.     if (a[i] == '\0') {
  286.       goto EXIT_1;
  287.     }
  288.     s->Chars.A[i + 1] = a[i];
  289.     INC(i);
  290.     if (i > (O_1 - 1)) {
  291.       goto EXIT_1;
  292.     }
  293.   } EXIT_1:;
  294.   s->Length = i;
  295.   FREE_OPEN_ARRAYS
  296. }
  297.  
  298. void Strings_StringToArray
  299. # ifdef __STDC__
  300. (Strings_tString *s, CHAR a[], LONGCARD O_2)
  301. # else
  302. (s, a, O_2)
  303. Strings_tString *s;
  304. CHAR a[];
  305. LONGCARD O_2;
  306. # endif
  307. {
  308.   Strings_tStringIndex i;
  309.  
  310.   {
  311.     Strings_tStringIndex B_11 = 1, B_12 = s->Length;
  312.  
  313.     if (B_11 <= B_12)
  314.       for (i = B_11;; i += 1) {
  315.         a[i - 1] = s->Chars.A[i];
  316.         if (i >= B_12) break;
  317.       }
  318.   }
  319.   a[s->Length] = '\0';
  320. }
  321.  
  322. INTEGER Strings_StringToInt
  323. # ifdef __STDC__
  324. (Strings_tString *s)
  325. # else
  326. (s)
  327. Strings_tString *s;
  328. # endif
  329. {
  330.   Strings_tStringIndex i, start;
  331.   INTEGER n;
  332.   BOOLEAN negative;
  333.  
  334.   switch (s->Chars.A[1]) {
  335.   case '+':;
  336.     negative = FALSE;
  337.     start = 2;
  338.     break;
  339.   case '-':;
  340.     negative = TRUE;
  341.     start = 2;
  342.     break;
  343.   default :
  344.     negative = FALSE;
  345.     start = 1;
  346.     break;
  347.   }
  348.   n = 0;
  349.   {
  350.     Strings_tStringIndex B_13 = start, B_14 = s->Length;
  351.  
  352.     if (B_13 <= B_14)
  353.       for (i = B_13;; i += 1) {
  354.         n = n * 10 + (INTEGER)(ORD(s->Chars.A[i]) - ORD('0'));
  355.         if (i >= B_14) break;
  356.       }
  357.   }
  358.   if (negative) {
  359.     return -n;
  360.   } else {
  361.     return n;
  362.   }
  363. }
  364.  
  365. CARDINAL Strings_StringToNumber
  366. # ifdef __STDC__
  367. (Strings_tString *s, CARDINAL Base)
  368. # else
  369. (s, Base)
  370. Strings_tString *s;
  371. CARDINAL Base;
  372. # endif
  373. {
  374.   Strings_tStringIndex i;
  375.   CARDINAL n;
  376.   CHAR ch;
  377.  
  378.   n = 0;
  379.   {
  380.     Strings_tStringIndex B_15 = 1, B_16 = s->Length;
  381.  
  382.     if (B_15 <= B_16)
  383.       for (i = B_15;; i += 1) {
  384.         ch = s->Chars.A[i];
  385.         if ('A' <= ch && ch <= 'F') {
  386.           n = n * Base + ORD(ch) - ORD('A') + 10;
  387.         } else if ('a' <= ch && ch <= 'f') {
  388.           n = n * Base + ORD(ch) - ORD('a') + 10;
  389.         } else {
  390.           n = n * Base + ORD(ch) - ORD('0');
  391.         }
  392.         if (i >= B_16) break;
  393.       }
  394.   }
  395.   return n;
  396. }
  397.  
  398. REAL Strings_StringToReal
  399. # ifdef __STDC__
  400. (Strings_tString *s)
  401. # else
  402. (s)
  403. Strings_tString *s;
  404. # endif
  405. {
  406.   REAL n;
  407.   LONGCARD Mantissa;
  408.   INTEGER Exponent;
  409.   BOOLEAN MantissaNeg;
  410.   BOOLEAN ExponentNeg;
  411.   CARDINAL FractionDigits;
  412.   CARDINAL TruncatedDigits;
  413.   CHAR ch;
  414.   Strings_tStringIndex i;
  415.  
  416.   MantissaNeg = FALSE;
  417.   Mantissa = 0;
  418.   Exponent = 0;
  419.   FractionDigits = 0;
  420.   TruncatedDigits = 0;
  421.   i = 0;
  422.   Strings_Append(s, ' ');
  423.   INC(i);
  424.   ch = s->Chars.A[i];
  425.   switch (ch) {
  426.   case '+':;
  427.     INC(i);
  428.     ch = s->Chars.A[i];
  429.     break;
  430.   case '-':;
  431.     INC(i);
  432.     ch = s->Chars.A[i];
  433.     MantissaNeg = TRUE;
  434.     break;
  435.   case 'E':;
  436.   case 'e':;
  437.     Mantissa = 1;
  438.     break;
  439.   default :
  440.     break;
  441.   }
  442.   while ('0' <= ch && ch <= '9') {
  443.     if (Mantissa <= MaxIntDiv10) {
  444.       Mantissa = 10 * Mantissa;
  445.       if (Mantissa <= MaxInt - (ORD(ch) - ORD('0'))) {
  446.         INC1(Mantissa, ORD(ch) - ORD('0'));
  447.       } else {
  448.         INC(TruncatedDigits);
  449.       }
  450.     } else {
  451.       INC(TruncatedDigits);
  452.     }
  453.     INC(i);
  454.     ch = s->Chars.A[i];
  455.   }
  456.   if (ch == '.') {
  457.     INC(i);
  458.     ch = s->Chars.A[i];
  459.   }
  460.   while ('0' <= ch && ch <= '9') {
  461.     if (Mantissa <= MaxIntDiv10) {
  462.       Mantissa = 10 * Mantissa;
  463.       if (Mantissa <= MaxInt - (ORD(ch) - ORD('0'))) {
  464.         INC1(Mantissa, ORD(ch) - ORD('0'));
  465.       } else {
  466.         INC(TruncatedDigits);
  467.       }
  468.     } else {
  469.       INC(TruncatedDigits);
  470.     }
  471.     INC(FractionDigits);
  472.     INC(i);
  473.     ch = s->Chars.A[i];
  474.   }
  475.   if (ch == 'E') {
  476.     INC(i);
  477.     ch = s->Chars.A[i];
  478.     switch (ch) {
  479.     case '+':;
  480.       ExponentNeg = FALSE;
  481.       INC(i);
  482.       ch = s->Chars.A[i];
  483.       break;
  484.     case '-':;
  485.       ExponentNeg = TRUE;
  486.       INC(i);
  487.       ch = s->Chars.A[i];
  488.       break;
  489.     default :
  490.       ExponentNeg = FALSE;
  491.       break;
  492.     }
  493.     while ('0' <= ch && ch <= '9') {
  494.       Exponent = 10 * Exponent + (INTEGER)(ORD(ch) - ORD('0'));
  495.       INC(i);
  496.       ch = s->Chars.A[i];
  497.     }
  498.     if (ExponentNeg) {
  499.       Exponent = -Exponent;
  500.     }
  501.   }
  502.   DEC1(Exponent, FractionDigits - TruncatedDigits);
  503.   n = FLOAT(Mantissa) * General_Exp10(Exponent);
  504.   if (MantissaNeg) {
  505.     return -n;
  506.   } else {
  507.     return n;
  508.   }
  509. }
  510.  
  511. void Strings_IntToString
  512. # ifdef __STDC__
  513. (INTEGER n, Strings_tString *s)
  514. # else
  515. (n, s)
  516. INTEGER n;
  517. Strings_tString *s;
  518. # endif
  519. {
  520.   INTEGER i, j;
  521.   CARDINAL length;
  522.   struct S_4 digits;
  523.  
  524.   if (n < 0) {
  525.     s->Chars.A[1] = '-';
  526.     j = 1;
  527.     n = -n;
  528.   } else {
  529.     j = 0;
  530.   }
  531.   length = 0;
  532.   do {
  533.     INC(length);
  534.     digits.A[length] = MyCHR.A[n % 10];
  535.     n = n / 10;
  536.   } while (!(n == 0));
  537.   for (i = (INTEGER)length; i >= 1; i += -1) {
  538.     INC(j);
  539.     s->Chars.A[j] = digits.A[i];
  540.   }
  541.   s->Length = j;
  542. }
  543.  
  544. void Strings_ReadS
  545. # ifdef __STDC__
  546. (IO_tFile f, Strings_tString *s, Strings_tStringIndex FieldWidth)
  547. # else
  548. (f, s, FieldWidth)
  549. IO_tFile f;
  550. Strings_tString *s;
  551. Strings_tStringIndex FieldWidth;
  552. # endif
  553. {
  554.   Strings_tStringIndex i;
  555.  
  556.   {
  557.     Strings_tStringIndex B_17 = 1, B_18 = FieldWidth;
  558.  
  559.     if (B_17 <= B_18)
  560.       for (i = B_17;; i += 1) {
  561.         s->Chars.A[i] = IO_ReadC(f);
  562.         if (i >= B_18) break;
  563.       }
  564.   }
  565.   s->Length = FieldWidth;
  566. }
  567.  
  568. void Strings_ReadL
  569. # ifdef __STDC__
  570. (IO_tFile f, Strings_tString *s)
  571. # else
  572. (f, s)
  573. IO_tFile f;
  574. Strings_tString *s;
  575. # endif
  576. {
  577.   Strings_tStringIndex i;
  578.   CHAR ch;
  579.  
  580.   i = 0;
  581.   for (;;) {
  582.     ch = IO_ReadC(f);
  583.     if (ch == EolCh) {
  584.       goto EXIT_2;
  585.     }
  586.     if (i == Strings_cMaxStrLength) {
  587.       do {
  588.       } while (!(IO_ReadC(f) == EolCh));
  589.       goto EXIT_2;
  590.     }
  591.     INC(i);
  592.     s->Chars.A[i] = ch;
  593.   } EXIT_2:;
  594.   s->Length = i;
  595. }
  596.  
  597. void Strings_WriteS
  598. # ifdef __STDC__
  599. (IO_tFile f, Strings_tString *s)
  600. # else
  601. (f, s)
  602. IO_tFile f;
  603. Strings_tString *s;
  604. # endif
  605. {
  606.   Strings_tStringIndex i;
  607.  
  608.   {
  609.     Strings_tStringIndex B_19 = 1, B_20 = s->Length;
  610.  
  611.     if (B_19 <= B_20)
  612.       for (i = B_19;; i += 1) {
  613.         IO_WriteC(f, s->Chars.A[i]);
  614.         if (i >= B_20) break;
  615.       }
  616.   }
  617. }
  618.  
  619. void Strings_WriteL
  620. # ifdef __STDC__
  621. (IO_tFile f, Strings_tString *s)
  622. # else
  623. (f, s)
  624. IO_tFile f;
  625. Strings_tString *s;
  626. # endif
  627. {
  628.   Strings_WriteS(f, s);
  629.   IO_WriteNl(f);
  630. }
  631.  
  632. void BEGIN_Strings()
  633. {
  634.   static BOOLEAN has_been_called = FALSE;
  635.  
  636.   if (!has_been_called) {
  637.     has_been_called = TRUE;
  638.  
  639.     BEGIN_IO();
  640.     BEGIN_General();
  641.     BEGIN_IO();
  642.     BEGIN_IO();
  643.  
  644.     MyCHR.A[0] = '0';
  645.     MyCHR.A[1] = '1';
  646.     MyCHR.A[2] = '2';
  647.     MyCHR.A[3] = '3';
  648.     MyCHR.A[4] = '4';
  649.     MyCHR.A[5] = '5';
  650.     MyCHR.A[6] = '6';
  651.     MyCHR.A[7] = '7';
  652.     MyCHR.A[8] = '8';
  653.     MyCHR.A[9] = '9';
  654.     MyCHR.A[10] = 'A';
  655.     MyCHR.A[11] = 'B';
  656.     MyCHR.A[12] = 'C';
  657.     MyCHR.A[13] = 'D';
  658.     MyCHR.A[14] = 'E';
  659.     MyCHR.A[15] = 'F';
  660.   }
  661. }
  662.